package com.cashmate.struts;

import com.cashmate.hibernate.dao.LoanapplicationDAO;
import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.pojo.Loanapplications;
import com.cashmate.hibernate.pojo.Loanverification;
import com.cashmate.utilities.ClassUtils;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Restrictions;

public class Request_EditRequest extends ActionSupport {

    private LoanverificationDAO loanapp = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private List<Loanverification> gridModel;
    //private List<Loanapplications> loanapps;
    private Integer rows = 0;
    private Integer page = 0;
    private Integer total = 0;
    private Integer record = 0;
    private String sord;
    private String sidx;
    private String searchField;
    private String searchString;
    private String searchOper;
    private boolean loadonce = false;
    private String selected;

    @Actions({
        @Action(value = "/editrequest", results = {
            @Result(name = "success", type = "json")
        })
    })
    @Override
    public String execute(){
        try {
            
            /**
             * eto ung ginawa natin. mukhang maayos naman na. :3
             */
                                  
            SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
            int to = (getRows() * getPage());
            int from = to - getRows();

            selected = (String) ServletActionContext.getRequest().getSession().getAttribute("selected");
            ServletActionContext.getRequest().getSession().removeAttribute("selected");
            //loanapps = new ArrayList<Loanapplications>();
            String[] sel = selected.split(",");
            Integer[] sel_int = new Integer[sel.length];
            int count = 0;
            for (String s : sel) {
                sel_int[count] = Integer.parseInt(s);
                count++;

            }


            DetachedCriteria criteria = DetachedCriteria.forClass(Loanverification.class);
            Criterion selec = Restrictions.in("loanverificationid", sel_int);
            criteria.add(selec); 

            record = loanapp.countByCriteria(criteria);
            criteria.setProjection(null);
            criteria.setResultTransformer(Criteria.ROOT_ENTITY);


            if (to > getRecord()) {
                to = getRecord();
            }

            if (loadonce) {
                setGridModel(loanapp.findByCriteria(criteria, from, to));
            } else {
                setGridModel(loanapp.findByCriteria(criteria, from, to));
            }

            for (Loanverification l : gridModel) {
                System.out.println(ClassUtils.toString(l));
            }

            setTotal((int) Math.ceil((double) getRecord() / (double) getRows()));
        } catch (Exception e) {
            return ERROR;
        } finally {
           // LoanapplicationDAO.close();
        }
        return SUCCESS;
    }

    public String getJSON(){
        return execute();
    }

    /**
     * @return how many rows we want to have into the grid
     */
    public Integer getRows() {
        return rows;
    }

    /**
     * @param rows
     *          how many rows we want to have into the grid
     */
    public void setRows(Integer rows) {
        this.rows = rows;
    }

    /**
     * @return current page of the query
     */
    public Integer getPage() {
        return page;
    }

    /**
     * @param page
     *          current page of the query
     */
    public void setPage(Integer page) {
        this.page = page;
    }

    /**
     * @return total pages for the query
     */
    public Integer getTotal() {
        return total;
    }

    /**
     * @param total
     *          total pages for the query
     */
    public void setTotal(Integer total) {
        this.total = total;
    }

    /**
     * @return total number of records for the query. e.g. select count(*) from
     *         table
     */
    public Integer getRecord() {
        return record;
    }

    /**
     * @param record
     *          total number of records for the query. e.g. select count(*) from
     *          table
     */
    public void setRecord(Integer record) {

        this.record = record;

        if (this.record > 0 && this.rows > 0) {
            this.total = (int) Math.ceil((double) this.record / (double) this.rows);
        } else {
            this.total = 0;
        }
    }

    /**
     * @return an collection that contains the actual data
     */
    public List<Loanverification> getGridModel() {
        return gridModel;
    }

    /**
     * @param gridModel
     *          an collection that contains the actual data
     */
    public void setGridModel(List<Loanverification> gridModel) {
        this.gridModel = gridModel;
    }

    /**
     * @return sorting order
     */
    public String getSord() {
        return sord;
    }

    /**
     * @param sord
     *          sorting order
     */
    public void setSord(String sord) {
        this.sord = sord;
    }

    /**
     * @return get index row - i.e. user click to sort.
     */
    public String getSidx() {
        return sidx;
    }

    /**
     * @param sidx
     *          get index row - i.e. user click to sort.
     */
    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public void setSearchOper(String searchOper) {
        this.searchOper = searchOper;
    }

    public void setLoadonce(boolean loadonce) {
        this.loadonce = loadonce;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }
}
